{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "r：表示文件只能读取\n",
    "w：表示文件只能写入\n",
    "a：表示打开文件，在原有内容的基础上追加内容，在末尾写入\n",
    "w+:表示可以对文件进行读写双重操作\n",
    "打开文件的方式，mode='r','w','a'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('./demo.txt', 'w') as f:\n",
    "    f.write('Hello,friends,\\n this if file read and write topic,\\n enjoy it')\n",
    "\n",
    "with open('./demo.txt') as f:\n",
    "    content = f.read()\n",
    "    print(content)\n",
    "\n",
    "with open('./demo.txt') as f:\n",
    "    line = f.readline()\n",
    "    while line != '':\n",
    "        line = f.readline()\n",
    "        print(line)\n",
    "\n",
    "with open('./demo.txt') as f:\n",
    "    lines = f.readlines()\n",
    "    for line in lines:\n",
    "        print(line)\n",
    "\n",
    "\n",
    "with open('./demo.txt', 'a') as f:\n",
    "    f.write('\\nlast line\\n')\n",
    "\n",
    "\n",
    "with open('./demo.txt', 'w+') as f:\n",
    "    f.write('Hello,friends,\\n this if file read and write topic,\\n enjoy it')\n",
    "    f.seek(0)\n",
    "    lines = f.readlines()\n",
    "    for line in lines:\n",
    "        print(line)    \n",
    "\n",
    "with open('./demo.txt', 'a+') as f:\n",
    "    f.write('Hello,friends,\\n this if file read and write topic,\\n enjoy it')\n",
    "    f.seek(0)\n",
    "    lines = f.readlines()\n",
    "    for line in lines:\n",
    "        print(line)         "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
